using System;
using Tools;

//字节数组内存池
namespace Core.Net
{
    public class ByteArrayPool : SimpleSingletonProvider<ByteArrayPool>
    {
        //线程锁
        private readonly object PoolLock = new object();
        //字节类缓存栈存储比较大的数据
        private ConcurrentQueue<byte[]> _bytPoolMore;
        //字节类缓存栈存储比较小的数据
        private ConcurrentQueue<byte[]> _bytPoolLess;
        //局部对象缓存
        private byte[] _buf = null;
        //入池最大字节
        public int TopByts = 10000;

        private int _testIndex;

        //重写初始化方法
        protected override void InstanceInit()
        {
            base.InstanceInit();
       
            this._bytPoolMore = new ConcurrentQueue<byte[]>();
            this._bytPoolLess = new ConcurrentQueue<byte[]>();

            //初始化池
            this.initQueue(66, 6);
        }
        //可初始化池内容
        public void initQueue(int _less, int _more)
        {
            this.addQueue(ref this._bytPoolLess, _less, true);
            this.addQueue(ref this._bytPoolMore, _more, false);
        }
        //添加子项到池中
        protected void addQueue(ref ConcurrentQueue<byte[]> _queue,int _len,bool _is_less)
        {
            for (int a = 0; a < _len; a++)
            {
                if (_is_less)
                {
                    _queue.Enqueue(new byte[100]);
                }
                else
                {
                    _queue.Enqueue(new byte[1000]);
                }
            }
        }
        // 获取对象
        public byte[] getItem(int _len)
        {
            if (_len <= 1000)
            {
                return this.getBufArray(ref this._bytPoolLess, _len);
            }
            return this.getBufArray(ref this._bytPoolMore, _len);
        }
        //获取缓存的数组数据
        protected byte[] getBufArray(ref ConcurrentQueue<byte[]> _bytPool,int _len)
        {
            lock (PoolLock)
            {
                if (_bytPool.Count > 0)
                {
                    //预防有空对象
                    while (_bytPool.Count > 0)
                    {
                        _buf = _bytPool.Dequeue();
                        // Debug.LogWarning("目前消耗到了{0}个", _bytPoolLess.Count);
                        if (_buf != null || _bytPool.Count <= 0)
                        {
                            break;
                        }
                    }
                    if (_buf != null)
                    {
                        if (_buf.Length < _len)
                        {

                            Array.Resize<byte>(ref _buf, _len);
                        }
                        return _buf;
                    }
                }

                return new byte[_len];
            }
        }

        //回收对象
        public byte[] storeItem(byte[] _item)
        {
            lock (PoolLock)
            {
                if (_item.Length < TopByts)
                {
                    if (_item.Length >= 1000)
                    {
                        this._bytPoolMore.Enqueue(_item);
                    }
                    else
                    {
                        this._bytPoolLess.Enqueue(_item);
                        // Debug.LogWarning("目前存入了{0}个", _bytPoolLess.Count);
                    }
                    return _item;
                }
                return null;
            }
        }
        //重写清理方法
        protected override void OnDestroyInstance()
        {
            base.OnDestroyInstance();
            destroy();
        }
        //清理内存池数据
        protected void destroy()
        {
            _bytPoolMore.Clear();
            _bytPoolLess.Clear();
            _bytPoolMore = null;
            _bytPoolLess = null;
        }
    }
}
